package com.itheima.article.consumer;

import com.alibaba.fastjson.JSON;
import com.itheima.article.mapper.ApArticleMapper;
import com.itheima.article.pojo.ApArticle;
import com.itheima.article.vo.ArticleRedisVo;
import com.itheima.behaviour.dto.UpdateArticleMess;
import com.itheima.common.constants.BusinessConstants;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
public class ArticleHotListener {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @KafkaListener(topics = BusinessConstants.MqConstants.HOT_ARTICLE_INCR_HANDLE_TOPIC)
    public void jieshou(ConsumerRecord<String, String> record) {
        String key = record.key();     // aritlceID_type
        String value = record.value();// 出现的次数
        System.out.println("key:>>>>>>>>>>" + key);
        System.out.println("value:>>>>>>>>>>>" + value);
        //业务代码  更新文章表的点赞数量 等
        //1.先根据ID 查询出文章的详情数据
        String[] s = key.split("_");
        Long articleId = Long.valueOf(s[0]);
        String type = s[1];
        UpdateArticleMess.UpdateArticleType typeEnum = UpdateArticleMess.UpdateArticleType.valueOf(type);

        Integer count = Integer.valueOf(value);

        ApArticle apArticle = apArticleMapper.selectById(articleId);

        if (apArticle != null) {
            //2.更新之后

            switch (typeEnum) {
                case VIEWS: {
                    if (apArticle.getViews() != null) {
                        apArticle.setViews(apArticle.getViews() + count);
                    } else {
                        apArticle.setViews(count);
                    }

                    break;
                }
                case LIKES: {
                    if (apArticle.getLikes() != null) {
                        apArticle.setLikes(apArticle.getLikes() + count);
                    } else {
                        apArticle.setLikes(count);
                    }
                    break;
                }
                case COMMENT: {
                    if (apArticle.getComment() != null) {
                        apArticle.setComment(apArticle.getComment() + count);
                    } else {
                        apArticle.setComment(count);
                    }

                    break;
                }
                case COLLECTION: {
                    if (apArticle.getCollection() != null) {
                        apArticle.setCollection(apArticle.getCollection() + count);
                    } else {
                        apArticle.setCollection(count);
                    }
                    break;
                }
                default: {
                    break;
                }
            }
            apArticleMapper.updateById(apArticle);
            //3.重新计算分值
            Integer score = computeScore(apArticle);
            //4.设置到redis中 保留30条

            ArticleRedisVo articleRedisVo = JSON.parseObject(JSON.toJSONString(apArticle), ArticleRedisVo.class);

            stringRedisTemplate
                    .opsForZSet().add(
                    BusinessConstants.ArticleConstants.HOT_ARTICLE_FIRST_PAGE+
                            apArticle.getChannelId(),
                    JSON.toJSONString(articleRedisVo),
                    score);
            //默认频道的ID 是0  默认频道的数据
            stringRedisTemplate
                    .opsForZSet().add(
                    BusinessConstants.ArticleConstants.HOT_ARTICLE_FIRST_PAGE+
                            BusinessConstants.ArticleConstants.DEFAULT_TAG,
                    JSON.toJSONString(articleRedisVo),
                    score);


            stringRedisTemplate
                    .opsForZSet().removeRange(BusinessConstants.ArticleConstants.HOT_ARTICLE_FIRST_PAGE +
                    apArticle.getChannelId(), 0, -31);

            stringRedisTemplate
                    .opsForZSet().removeRange(
                    BusinessConstants.ArticleConstants.HOT_ARTICLE_FIRST_PAGE+
                            BusinessConstants.ArticleConstants.DEFAULT_TAG,
                    0,-31);
        }
    }


    private Integer computeScore(ApArticle apArticle) {
        Integer score = 0;
        if (apArticle.getLikes() != null) {
            score += apArticle.getLikes() * BusinessConstants.ArticleConstants.HOT_ARTICLE_LIKE_WEIGHT;
        }
        if (apArticle.getCollection() != null) {
            score += apArticle.getCollection() * BusinessConstants.ArticleConstants.HOT_ARTICLE_COLLECTION_WEIGHT;
        }
        if (apArticle.getComment() != null) {
            score += apArticle.getComment() * BusinessConstants.ArticleConstants.HOT_ARTICLE_COMMENT_WEIGHT;
        }

        if (apArticle.getViews() != null) {
            score += apArticle.getViews();
        }
        return score;
    }
}
